<?php

namespace StudyBuddy\Controllers;

use \StudyBuddy\WebPage;
use \StudyBuddy\Template\Urhere;
use \StudyBuddy\SearchFactory;

class Search extends WebPage
{

	protected $aRequired = array('q');

	/**
	 * Search term
	 *
	 * @var string
	 */
	protected $term;

	/**
	 * Pagination links on the page
	 * will not be handled by Ajax
	 *
	 * @var bool
	 */
	protected $notAjaxPaginatable = true;
	
	//protected $bRequirePost = true;

	/**
	 * (non-PHPdoc)
	 * @see StudyBuddy.WebPage::main()
	 */
	protected function main(){
		/**
		 * Do NOT run urldecode() on request string
		 * as it's already decoded because it uses
		 *  $_GET as underlying array, and php
		 *  already decodes $_GET or $_POST vars
		 */
		$this->term = $this->oRegistry->Request->getUTF8('q')->stripTags();
		$this->aPageVars['qheader'] = '<h1>Search results for: '.$this->term.'</h1>';

		$this->aPageVars['title'] = 'Questions matching &#39;'.$this->term.'&#39;';
		d('$this->term: '.$this->term);

			
		$this->oSearch = SearchFactory::factory($this->oRegistry);
		$this->oSearch->search($this->term);

		$this->makeTopTabs()
		->makeInfo()
		->makeBody();
	}

	protected function makeTopTabs(){
            $guest = null;
            if($this->oRegistry->Viewer->isGuest()) {
                $guest['link_ask'] = '#'; // 13
                $guest['link_ask_click'] = ' onclick="oSL.getQuickRegForm()"'; //14
            }
		$tabs = Urhere::factory($this->oRegistry)->get('tplToptabs', 'questions', $guest);
		$this->aPageVars['topTabs'] = $tabs;

		return $this;
	}


	protected function sendCacheHeaders(){


		return $this;
	}


	protected function makeInfo(){

		$this->aPageVars['side'] = \tplSearchInfo::parse(array($this->oSearch->count(), $this->term, 'questions matching'), false);


		return $this;
	}



	protected function makeBody(){


		$this->aPageVars['body'] = \tplQlist::parse(array('', $this->oSearch->getHtml(), $this->oSearch->getPagerLinks(), $this->notAjaxPaginatable), false);

		return $this;
	}


}
